package com.weyoung.app.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weyoung.app.dto.req.UserUpdateReqDTO;
import com.weyoung.app.dto.resp.UserDetailRespDTO;
import com.weyoung.app.mapper.UserDetailMapper;
import com.weyoung.app.mapper.UserMapper;
import com.weyoung.app.service.UserDetailService;
import com.weyoung.app.service.UserService;
import com.weyoung.common.consatant.exception.ClientException;
import com.weyoung.common.enums.UserErrorCodeEnum;
import com.weyoung.common.model.entity.UserDO;
import com.weyoung.common.model.entity.UserDetailDO;
import com.weyoung.common.utils.AES;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetailDO> implements UserDetailService {

    private final UserDetailMapper userDetailMapper;

    @Override
    public UserDetailRespDTO userDetail() {
        String userId = StpUtil.getLoginId().toString();
        if (userId == null) {
            throw new ClientException(UserErrorCodeEnum.USER_TOKEN_FAIL);
        }
        UserDetailRespDTO detailRespDTO = userDetailMapper.getUserDetailById(userId);
        if (detailRespDTO == null) {
            throw new ClientException(UserErrorCodeEnum.USER_DETAIL_NULL);
        }
        try {
            if (detailRespDTO.getEmail() != null && !detailRespDTO.getEmail().isEmpty()) {
                detailRespDTO.setEmail(AES.decrypt(detailRespDTO.getEmail()));
            }
            if (detailRespDTO.getPhone() != null && !detailRespDTO.getPhone().isEmpty()) {
                detailRespDTO.setPhone(AES.decrypt(detailRespDTO.getPhone()));
            }
            if (detailRespDTO.getAddressInfo() != null && !detailRespDTO.getAddressInfo().isEmpty()) {
                detailRespDTO.setAddressInfo(AES.decrypt(detailRespDTO.getAddressInfo()));
            }
        } catch (Exception e) {
            throw new ClientException(UserErrorCodeEnum.USER_DETAIL_MESSAGE_ERROR);
        }

        // 检查除了 userId 之外的所有字段是否都为空
        if (areAllFieldsEmpty(detailRespDTO)) {
            throw new ClientException("未设置用户信息");
        }
        return detailRespDTO;
    }

    @Override
    public void updateUserDetail(UserUpdateReqDTO requestParam) {
        String userId = StpUtil.getLoginId().toString();
        if (userId == null) {
            throw new ClientException(UserErrorCodeEnum.USER_TOKEN_FAIL);
        }
        LambdaUpdateWrapper<UserDetailDO> updateWrapper = Wrappers.lambdaUpdate(UserDetailDO.class)
                .eq(UserDetailDO::getUserId, userId);

        UserDetailDO userDetailDO = UserDetailDO.builder()
                .userId(Integer.parseInt(userId))
                .realName(requestParam.getRealName())
                .gender(requestParam.getGender())
                .phone(requestParam.getPhone())
                .email(requestParam.getEmail())
                .birthDate(requestParam.getBirthDate())
                .addressInfo(requestParam.getAddressInfo())
                .build();

        baseMapper.update(userDetailDO, updateWrapper);
    }

    private boolean areAllFieldsEmpty(UserDetailRespDTO detailRespDTO) {
        return (detailRespDTO.getRealName() == null || detailRespDTO.getRealName().isEmpty()) &&
               (detailRespDTO.getGender() == null || detailRespDTO.getGender().isEmpty()) &&
               (detailRespDTO.getPhone() == null || detailRespDTO.getPhone().isEmpty()) &&
               (detailRespDTO.getEmail() == null || detailRespDTO.getEmail().isEmpty()) &&
               (detailRespDTO.getAddressInfo() == null || detailRespDTO.getAddressInfo().isEmpty());
    }
}
